//
//  VCInformationMoudle.m
//  VC
//
//  Created by 肖康龙 on 2017/8/30.
//  Copyright © 2017年 vsoontech. All rights reserved.


#import "VCInformationMoudle.h"
#import "CodeHelper.h"
#import "AFNetworking.h"
#define URL_string @"http://res.test-entry.vsooncloud.com/v3/vcudp"

@implementation VCInformationMoudle


/*获取秘钥*/
-(void)getKeyWithsucceed:(void(^)(id data))succeed fail:(void (^)(NSError *))fail{
    
//    NSString  *urlstring = @"https://192.168.1.199/v2/live/kl";
    NSString  *urlstring = @"https://p.linkinme.com/v2/live/kl";

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[CodeHelper AESDecryptIWithType:3 isMobileTV:NO] forKey:@"i"];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"text/json",
                                                         @"text/javascript",
                                                         @"text/plain",
                                                         @"video/mpegurl",
                                                         nil];
    AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    //是否可以使用自建证书（不花钱的）
    securityPolicy.allowInvalidCertificates=YES;
    //是否验证域名（一般不验证）
    securityPolicy.validatesDomainName=NO;
    
    [manager setSecurityPolicy:securityPolicy];
    [manager GET:urlstring parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dictsss = [CodeHelper decryptionKeyWithString:responseObject];
        
        if (succeed) {
            succeed(dictsss);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];

}


/*获取下载list*/
-(void)getVideoListWithLinkId:(NSString *)linkId
                      succeed:(void(^)(id data ))succeed
                         fail:(void(^)(NSError *error))fail{
    
    
//    NSString  *urlstring = [NSString stringWithFormat:@"http://192.168.1.199:8001/v3/vcudp/resource"];
    NSString  *urlstring = [NSString stringWithFormat:@"%@/resource",URL_string];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[CodeHelper AESDecryptIWithType:3 isMobileTV:NO] forKey:@"i"];
    [params setObject:[CodeHelper getSoucressListCodeID:linkId] forKey:@"e"];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"text/json",
                                                         @"text/javascript",
                                                         @"text/plain",
                                                         @"video/mpegurl",
                                                         nil];
    AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    //是否可以使用自建证书（不花钱的）
    securityPolicy.allowInvalidCertificates=YES;
    //是否验证域名（一般不验证）
    securityPolicy.validatesDomainName=NO;
    
    [manager setSecurityPolicy:securityPolicy];
    [manager GET:urlstring parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *dictsss = [CodeHelper decryptionPlayDataWithString:responseObject];
        if (succeed) {
            succeed(dictsss);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
        
    }];
    
}

/*加速接口*/
-(NSURLSessionDataTask *)getAccelerateWithLinkId:(NSString *)linkId
                     urlString:(NSString *)urlString
                       succeed:(void(^)(id data ))succeed
                          fail:(void(^)(NSError *error))fail{
    
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:linkId forKey:@"fid"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"text/json",
                                                         @"text/javascript",
                                                         @"text/plain",
                                                         @"video/mpegurl",
                                                         @"vedio/split",
                                                         nil];
    AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    //是否可以使用自建证书（不花钱的）
    securityPolicy.allowInvalidCertificates=YES;
    //是否验证域名（一般不验证）
    securityPolicy.validatesDomainName=NO;
    
    [manager setSecurityPolicy:securityPolicy];
    
    
    NSURLSessionDataTask *task = [manager GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if (succeed) {
            succeed(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
        
    }];
    return task;
}


/*流量上报
 e.appKey	appkey	 String
 e.resourceID	资源id	String
 e.playTime	播放时长，单位秒	Int
 e.accelSize	加速流量，单位字节	Long
 e.vcSize	Vc总的有效流量	Long
 e.vc		数组
 e.vc.host	Vc节点host	String
 e.vc.tSize	Vc节点总流量，单位字节	Long
 e.vc.vSize	Vc节点有效流量，单位字节	Long
 */
-(void)getReportWithResourceID:(NSString *)resourceID
                      playTime:(NSUInteger)playTime
                     accelSize:(NSUInteger)accelSize
                        vcSize:(NSUInteger)vcSize
                            vc:(NSArray *)vc
                       succeed:(void(^)(id data ))succeed
                          fail:(void(^)(NSError *error))fail{
    
    NSString  *urlstring = [NSString stringWithFormat:@"%@/flow",URL_string];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[CodeHelper AESDecryptIWithType:3 isMobileTV:NO] forKey:@"i"];
    [params setObject:[CodeHelper getReportWithResourceID:resourceID
                                                 playTime:playTime
                                                accelSize:accelSize
                                                   vcSize:vcSize
                                                       vc:vc] forKey:@"e"];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"text/json",
                                                         @"text/javascript",
                                                         @"text/plain",
                                                         @"video/mpegurl",
                                                         nil];
    AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    //是否可以使用自建证书（不花钱的）
    securityPolicy.allowInvalidCertificates=YES;
    //是否验证域名（一般不验证）
    securityPolicy.validatesDomainName=NO;
    
    [manager setSecurityPolicy:securityPolicy];
    [manager GET:urlstring parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *dictsss = [CodeHelper decryptionPlayDataWithString:responseObject];
        if (succeed) {
            succeed(dictsss);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
        
    }];
}

@end
